package com.xiyu.service.service.infra.devops;

import com.xiyu.service.vo.infra.devops.project.ProjectListQueryOutput;
import com.xiyu.service.vo.infra.devops.project.ProjectSingleGetOutput;
import com.xiyu.service.vo.infra.devops.project.ProjectUpdateInput;
import com.xiyu.service.vo.infra.devops.project.ProjectCreateInput;
import com.xiyu.service.model.devops.project.DevopsProject;
import com.xiyu.service.repository.devops.project.DevopsProjectRepository;
import com.xiyu.service.vo.infra.devops.project.ProjectPageQueryOutput;
import com.xiyu.service.vo.infra.devops.project.ProjectPageQueryInput;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.babyfish.jimmer.Page;
import java.util.*;

import org.springframework.transaction.annotation.Transactional;
import com.xiyu.service.util.entity.EntityUtils;
import com.xiyu.service.framework.web.web.core.pojo.PageResult;
import com.xiyu.service.convert.infra.devops.ProjectConvert;

import static com.xiyu.service.framework.exception.util.ServiceExceptionUtil.exception;
import static com.xiyu.service.errorCode.infra.devops.ProjectErrorCode.*;

/**
 * 项目管理 Service 实现类
 */
@Service
@Validated
public class ProjectServiceImpl implements ProjectService {

    @Resource
    private DevopsProjectRepository devopsProjectRepository;




    @Override
    public PageResult<ProjectPageQueryOutput> pageQuery(ProjectPageQueryInput inputVO) {
        Page<DevopsProject> pageDevopsProject = devopsProjectRepository.pageQuery(inputVO);
        List<ProjectPageQueryOutput> listDevopsProject = ProjectConvert.INSTANCE.pageQueryOutputConvert(pageDevopsProject.getRows());
        return new PageResult<>(listDevopsProject, pageDevopsProject.getTotalRowCount());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long create(ProjectCreateInput inputVO) {
        DevopsProject newDevopsProject = ProjectConvert.INSTANCE.createInputConvert(inputVO);
        newDevopsProject = devopsProjectRepository.insert(newDevopsProject);
        return newDevopsProject.id();

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(ProjectUpdateInput inputVO) {
        Optional<DevopsProject> optionalOldDevopsProject = devopsProjectRepository.findById(inputVO.getId());
        if(optionalOldDevopsProject.isEmpty()){
            throw exception(DEVOPS_PROJECT_NOT_EXIST);
        }
        DevopsProject updateDevopsProject = ProjectConvert.INSTANCE.updateInputConvert(inputVO);
        if (!EntityUtils.isEquals(optionalOldDevopsProject.get(), updateDevopsProject))
            devopsProjectRepository.update(updateDevopsProject);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleted(Long id) {
        Optional<DevopsProject> optionalOldDevopsProject = devopsProjectRepository.findById(id);
        if(optionalOldDevopsProject.isEmpty()){
            throw exception(DEVOPS_PROJECT_NOT_EXIST);
        }
        devopsProjectRepository.deleteById(id);
        return true;
    }

    @Override
    public ProjectSingleGetOutput singleGet(Long id) {
        Optional<DevopsProject> optionDevopsProject = devopsProjectRepository.findById(id);
        if(optionDevopsProject.isEmpty()){
            throw exception(DEVOPS_PROJECT_NOT_EXIST);
        }
        return ProjectConvert.INSTANCE.singleGetOutputConvert(optionDevopsProject.get());

    }

    @Override
    public List<ProjectListQueryOutput> listQuery() {
        List<DevopsProject> listDevopsProject = devopsProjectRepository.listQuery();
        return ProjectConvert.INSTANCE.listQueryOutputConvert(listDevopsProject);

    }

}
